package br.pucminas.virtual.validator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.EditText;
import br.com.caelum.stella.format.CNPJFormatter;
import br.com.caelum.stella.format.CPFFormatter;
import br.com.caelum.stella.format.RenavamFormatter;
import br.com.caelum.stella.format.TituloEleitoralFormatter;
import br.com.caelum.stella.validation.CNPJValidator;
import br.com.caelum.stella.validation.CPFValidator;
import br.com.caelum.stella.validation.InvalidStateException;
import br.com.caelum.stella.validation.RenavamValidator;
import br.com.caelum.stella.validation.TituloEleitoralValidator;

public class Validator {

	public static boolean validateNotNull(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				return true;
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;

	}

	@SuppressLint("SimpleDateFormat")
	public static boolean validateDateFormat(EditText editText,
			String dateFormat, String message) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				SimpleDateFormat format = new SimpleDateFormat(dateFormat);
				try {
					format.parse(strText);
					return true;
				} catch (ParseException pe) {
				}
			}
		}
		editText.setError(message);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validateCpf(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				try {
					strText = strText.replaceAll("[^0-9]", "");
					CPFFormatter formatter = new CPFFormatter();
					strText = formatter.format(strText);
					CPFValidator validator = new CPFValidator();
					validator.assertValid(strText);
					editText.setText(strText);
					return true;
				} catch (InvalidStateException e) {
				} catch (IllegalArgumentException e) {
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;

	}

	public static boolean validateCnpj(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				try {
					strText = strText.replaceAll("[^0-9]", "");
					CNPJFormatter formatter = new CNPJFormatter();
					strText = formatter.format(strText);
					CNPJValidator validator = new CNPJValidator();
					validator.assertValid(strText);
					editText.setText(strText);
					return true;
				} catch (InvalidStateException e) {
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;

	}

	public static boolean validateRenavam(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				try {
					strText = strText.replaceAll("[^0-9]", ""); // TODO verify
																// if only
																// digits
					RenavamFormatter formatter = new RenavamFormatter();
					strText = formatter.format(strText);
					RenavamValidator validator = new RenavamValidator();
					validator.assertValid(strText);
					editText.setText(strText);
					return true;
				} catch (InvalidStateException e) {
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validateTituloEleitor(EditText editText,
			String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				try {
					strText = strText.replaceAll("[^0-9]", ""); // TODO vrify if
																// only digits
					TituloEleitoralFormatter formatter = new TituloEleitoralFormatter();
					strText = formatter.format(strText);
					TituloEleitoralValidator validator = new TituloEleitoralValidator();
					validator.assertValid(strText);
					editText.setText(strText);
					return true;
				} catch (InvalidStateException e) {
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validateEmail(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				if (android.util.Patterns.EMAIL_ADDRESS.matcher(strText)
						.matches()) {
					return true;
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}
	
	public static boolean validatePhoneNumber(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				if (android.util.Patterns.PHONE.matcher(strText)
						.matches()) {
					return true;
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validateIpAddress(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				if (android.util.Patterns.IP_ADDRESS.matcher(strText).matches()) {
					return true;
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validatePhoneAdress(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				if (android.util.Patterns.PHONE.matcher(strText).matches()) {
					return true;
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;
	}

	public static boolean validatePassword(EditText editText, String pMessage) {
		Editable text = editText.getText();
		if (text != null) {
			String strText = text.toString();
			if (!TextUtils.isEmpty(strText)) {
				String passwordPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})";
				if (Pattern.compile(passwordPattern).matcher(strText).matches()) {
					return true;
				}
			}
		}
		editText.setError(pMessage);
		editText.setFocusable(true);
		// editText.requestFocus();
		return false;

	}

}
